package one.three.fourty;

public class QueueBeta<Item> {

	Node<Item> first;
	private int n;

	public void enqueue(Item item) {
		if (first == null) {
			first = new Node<Item>();
			first.value = item;
		} else {
			delete(item);
			Node<Item> n = new Node<Item>();
			n.value = item;
			n.next = first;
			first.pre = n;
			first = n;
		}
		n++;
	}

	public void delete(Item item) {
		Node<Item> c = first;
		while (c != null) {
			if (c.value.equals(item)) {
				if (c.pre != null) {
					c.pre.next = c.next;
				}
				if (c.next != null) {
					c.next.pre = c.pre;
				}
				n--;
			}
			c = c.next;
		}
	}

	@Override
	public String toString() {
		StringBuilder s = new StringBuilder();
		Node<Item> c = first;
		s.append("<");
		while (c != null) {
			s.append("[" + c.value + "]");
			c = c.next;
		}
		s.append(")");
		return s.toString();
	}

	public static void main(String[] args) {
		QueueBeta<String> q = new QueueBeta<String>();
		q.enqueue("a");
		q.enqueue("b");
		q.enqueue("c");
		q.enqueue("d");
		q.enqueue("a");
		System.out.println(q);
	}

}
